//
//  LFRefreshHeader.m
//  AntMarket
//
//  Created by LF on 2017/3/30.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFRefreshHeader.h"

@implementation LFRefreshHeader

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    UIScrollView *scrollView = (UIScrollView *)self.superview;
    switch (state) {
        case MJRefreshStateIdle:
            
            scrollView.scrollEnabled = YES;
            self.stateLabel.text = @"下拉可以刷新";
            break;
        case MJRefreshStatePulling:
            
            scrollView.scrollEnabled = YES;
            self.stateLabel.text = @"松开立即刷新";
            break;
        case MJRefreshStateWillRefresh:
            
            scrollView.scrollEnabled = YES;
//            self.stateLabel.text = FGGetStringWithKeyFromTable(@"下拉可以刷新", @"NeedLocationLanguage");
            break;
        case MJRefreshStateRefreshing:
            
//            scrollView.scrollEnabled = NO;
            self.stateLabel.text = @"正在刷新数据中";
            break;
        default:
            break;
    }
}

-(void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey{
    [super setLastUpdatedTimeKey:lastUpdatedTimeKey];
    
    // 如果label隐藏了，就不用再处理
    if (self.lastUpdatedTimeLabel.hidden) return;
    
    NSDate *lastUpdatedTime = [[NSUserDefaults standardUserDefaults] objectForKey:lastUpdatedTimeKey];
    
    // 如果有block
    if (self.lastUpdatedTimeText) {
        self.lastUpdatedTimeLabel.text = self.lastUpdatedTimeText(lastUpdatedTime);
        return;
    }
    
    if (lastUpdatedTime) {
        // 1.获得年月日
        NSCalendar *calendar = [self currentCalendar];
        NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
        NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
        NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
        
        // 2.格式化日期
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        BOOL isToday = NO;
        if ([cmp1 day] == [cmp2 day]) { // 今天
            formatter.dateFormat = @" HH:mm";
            isToday = YES;
        } else if ([cmp1 year] == [cmp2 year]) { // 今年
            formatter.dateFormat = @"MM-dd HH:mm";
        } else {
            formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        }
        NSString *time = [formatter stringFromDate:lastUpdatedTime];
        NSString *sss =[NSBundle mj_localizedStringForKey:MJRefreshHeaderNoneLastDateText];
        
        // 3.显示日期
        self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@%@",
                                          @"最后更新:",
                                          isToday ? @"今天" : @"",
                                          time];
    } else {
        self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@%@",
                                          @"最后更新:",
                                          @"无记录"];
    }
}
#pragma mark - 日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。
- (NSCalendar *)currentCalendar {
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    return [NSCalendar currentCalendar];
}
@end
